<?php
/**
 * Created by PhpStorm.
 * User: w
 * Date: 2016/6/20
 * Time: 16:28
 */

//后台最美医生投票--推荐信息
class RecommendController extends BaseController{

    function index(){
        $do_re = new DoVote_recommend_doctor();
        $page = new tools\Page($do_re->count(),10);
        $vote_list = $do_re->offset($page->limit)->limit($page->pagesize)->orderby('id desc')->rows();
        $vote_form_url = $this->url('recommend','vote_form');
        require $this->template;
    }

    //信息表单处理
    function vote_form(){
        $do_re = new DoVote_recommend_doctor();
        $do_note = new DoNote_record();
        $list_url = $this ->url('recommend','index');
        if($_POST){
            $do_re->set_id($_GET['id']);
            $dc_name= $this->request('dc_name',"",true);
            $dc_descript = $this->request('dc_descript',"",true);
            $do_re->set_dc_name($dc_name);
            $do_re->set_dc_descript($dc_descript);
            $do_re->set_referrer($this->request('referrer')) ;
            $do_re->set_referrer_tel($this->request("referrer_tel"));
            $do_re->set_state($this->request('state'));
            if($do_re->save()){
                $send_tel = $this->request('send_tel');
                $send_tel_content = $this->request('send_tel_content');
                $send_re = $this->request('send_re');
                $send_re_content = $this->request('send_re_content');
                //给被推荐人发送
             if(!empty($send_tel) && !empty($send_tel_content)){
                   $res = Tools::send_text_messages($send_tel_content,$send_tel);
                   $do_note->set_content($send_tel_content);
                   $do_note->set_tel($send_tel);
                   $do_note->set_type("1");
                   $do_note->set_time(date('Y-m-d H:i:s',time()));
                   $do_note->save();
                 //  echo "<script>alert('短信发送成功！');</script>";
                }
              //给推荐人发送短信
                if(!empty($send_re) && !empty($send_re_content)){
                    $res1 = Tools::send_text_messages($send_re_content,$send_re);
                    $do_note->set_content($send_re_content);
                    $do_note->set_tel($send_re);
                    $do_note->set_type("1");
                    $do_note->set_time(date('Y-m-d H:i:s',time()));
                    $do_note->save();
                  //  echo "<script>alert('短信发送成功！');</script>";
                }
                echo "<script>alert('提交成功！');location.href='".$list_url."'</script>";
            }else{
                echo "<script>alert('提交失败！');</script>";
            }

        }else{
            $id = $_GET['id'];
            $do_re->set_id($id);
            $info = $do_re->where($do_re->data)->row();
            $ajax_url = $this->url('vote','ajax_sent_phone');
            $state = array('待审核','通过','未通过');
            require $this->template;
        }


    }

    //推送消息
    function  ajax_sent_phone(){
        $bm = new C发微信消息();
        $infoId = $this->request('id','',true);
        $openId = $this->request('oid');

        $doInformation = new DoVote_beautiful_doctor();
        $doInformation->set_id($infoId);
        $doInformation->set_state('1');

        $doDetail = new Do咨询详情();
        $doDetail->set_咨询id($infoId);
        $res = $doDetail->where($doDetail->data)->count();
        if($openId){
            $msg = '测试通过';
        }else{
            $msg = '推送完毕';
        }
        $bm->ff发送咨询信息($infoId,$openId);
        $this->jsonOutPut(0, $msg);
        /*if($res>0){
            if (!$doInformation->where($doInformation->data)->count() and !$openId) {
                $doInformation->save();
            }
            $bm->ff发送咨询信息($infoId,$openId);
            $this->jsonOutPut(0, $msg);
        }else{
            $this->jsonOutPut(-1, '请添加图文消息');
        }*/
    }

    //给未收到消息的用户重推
    public function accept_failed_user(){
        $bm = new C发微信消息();
        $id = $this->request('id');
        $bm->ff发送咨询信息($id,'','失败');
        echo true;
    }

}